
// 等级任务 组件
import React, {useRef} from 'react';
import {ModalForm} from "@ant-design/pro-form";
import {message, Switch} from "antd";
import ProTable from "@ant-design/pro-table";
import { distributionTaskLevelDelete } from "@/services/ant-design-pro/distribution";
import {getDistributionTaskLevel} from "@/services/ant-design-pro/distribution";
import TaskLevelUpdate from "@/pages/distribution/DistributionLevel/components/TaskLevelUpdate";

const TaskLevel = () => {
  const actionRef = useRef()

  // 定义等级任务删除 方法
  const deleteTask = async (id)=>{
    await distributionTaskLevelDelete(id).then(()=>{
      message.success(`删除第${id}条成功`);
      actionRef.current?.reload()
    })
  }

  // 定义表头字段
  const columns = [
    {
      title: 'ID',
      // width: 80,
      dataIndex: 'id',
      key:'id'
    },
    {
      title: '名称',
      dataIndex: 'task_name',
      key:'task_name'
    },
    {
      title: '任务类型',
      dataIndex: 'task_type',
      key:'task_type'
    },
    {
      title: '限定数量',
      dataIndex: 'limit_num',
      key:'limit_num'
    },
    {
      title: '是否显示',
      dataIndex: 'is_show',
      key:'is_show',
      render: (_,record) =>(
        <Switch defaultChecked={record.is_show}  key='is_show' checkedChildren="显示" unCheckedChildren="隐藏" />
      )
    },
    {
      title: '排序',
      dataIndex: 'sort',
      key:'sort'
    },
    {
      title: '操作',
      align:'center',
      valueType: 'option',
      fixed: 'right',
      key: 'option',
      render: (_,record) => [
        <TaskLevelUpdate actionRef={actionRef} record={record} />,
        <a onClick={()=>{deleteTask(record.id)}} key="link2"> 删除 </a>,
      ],
    },
  ]
  return (
    <div>
      <ModalForm
        title="等级任务"
        trigger={
          <span><a key="link1"> 等级任务 </a>  </span>
        }
      >

        <ProTable
          actionRef={actionRef}
          columns={columns}
          request={async (params)=>{
            const {data,meta} = await getDistributionTaskLevel(params)
            return{
              data,
              success:true,
              total:meta.pagination.total
            }
          }}
          pagination={{
            // showQuickJumper: true,
            pageSize: 5,
          }}
          rowKey="key"
          options={false}
          search={false}
          dateFormatter="string"
        />

      </ModalForm>
    </div>
  );
};

export default TaskLevel;
